package com.zghhost.codedemo.charset;


import java.util.Arrays;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/4/30 11:22
 */
public class CharsetTest {
    public static void main(String[] args)throws Exception {
        //当前使用IEDA java文件编码和javac编译时 都是UTF-8
        for(long i=Long.MAX_VALUE - 10000,ic=Long.MAX_VALUE;i<ic;i++){
//            String binayStr = String.format("%016d",Long.valueOf(Long.toBinaryString(i)));
//            String decimalismStr = String.format("% 16d",i);
            char c = (char)i;
            String sc = ""+c;
            System.out.print(sc +"(\\u"+Long.toHexString(c)+")"+"("+ Arrays.toString(sc.getBytes())+")"+"("+getStringBinaryData(sc,"UTF-8")+")");

            if(i % 10 == 0){
                System.out.println();
            }
        }

        System.out.println();
        System.out.println('\u37b8');

        System.out.println(0xFF);
        System.out.println(0xFFF);
        System.out.println(0xFFFF);


        System.out.println("-------");

        String s = new String(new byte[]{-16, -65, -70,-65});
        System.out.println(s);
        System.out.println(s.length());
        s.codePoints().forEach(i -> System.out.println(i));
        System.out.println(s.toCharArray().length);
        for(char c : s.toCharArray()){
            System.out.println(Long.toHexString(c));
        }
        System.out.println(getStringBinaryData(s,"UTF-8"));
    }

    public static String getStringBinaryData(String s,String charsetStr)throws Exception {
        StringBuffer sb = new StringBuffer();
        byte[] bs = s.getBytes(charsetStr);
        for(byte b : bs){
            if(b >= 0){
                sb.append(String.format("%08d",Long.valueOf(Long.toBinaryString(b))));
            }else{
                sb.append(String.format("%08d",Long.valueOf(Long.toBinaryString(b).substring(56))));
            }
        }
        return sb.toString();
    }
}

